/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.beans.CPF;
import model.beans.Paciente;
import model.beans.RG;
import model.beans.TipoConvenio;
import model.persistence.GerenciadorEntidades;

/**
 *
 * @author kelyane
 */
@WebServlet(name = "PacienteServlet", urlPatterns = {"/PacienteServlet"})
public class PacienteServlet extends HttpServlet {

    
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String action = request.getParameter("action");
        String page = request.getParameter("page");
              
        try {
                        
            switch(page){
                case  "listar" : {
                    listPaciente(request,response);
                    break;
                }
                case "insert" :{
                    insertPaciente(request,response);
                    break;
                }               
                case "excluir" :{                    
                    excluirPaciente(request,response);
                    break;
                }                      
                case "renderEditar" : {
                    renderPaciente(request,response);
                    break;
                }
                case "editar" :{
                    editar(request,response);
                    break;
                }
            }
            
           
           
        } finally {            
            out.close();
        }
    }
    
    
    public void insertPaciente(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String nome = request.getParameter("nomePaciente");
        String rg = request.getParameter("rgPaciente");
        String cpf = request.getParameter("cpfPaciente");
        String dataNasc = request.getParameter("dataNascPaciente");
        String email = request.getParameter("emailPaciente");
        String endereco = request.getParameter("enderecoPaciente");
        String fone = request.getParameter("telPaciente");
        String cidade = request.getParameter("cidPaciente");
        String estado = request.getParameter("ufPaciente");
        
        Paciente paciente = new Paciente(nome, new RG(rg), new CPF(cpf), new Date(dataNasc), endereco, estado, cidade, fone, email, TipoConvenio.planoSaude);
        
        GerenciadorPaciente.cadastrarPaciente(paciente);     
        
        RequestDispatcher view = request.getRequestDispatcher("PacienteServlet?page=listar");        
        view.forward(request, response);
    }
    

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private void listPaciente(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EntityManager em = GerenciadorEntidades.getEntityManager();
        Query query = em.createNamedQuery("findAllPaciente");

        List<Paciente> pacientesNamedQuery = query.getResultList();
        RequestDispatcher view = request.getRequestDispatcher("list-paciente.jsp");
        request.setAttribute("pacientes", pacientesNamedQuery);
        view.forward(request, response);
    }

    private void editar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String nome = request.getParameter("nomePaciente");
        String rg = request.getParameter("rgPaciente");
        String cpf = request.getParameter("cpfPaciente");
        String dataNasc = request.getParameter("dataNascPaciente");
        String email = request.getParameter("emailPaciente");
        String endereco = request.getParameter("enderecoPaciente");
        String fone = request.getParameter("telPaciente");
        String cidade = request.getParameter("cidPaciente");
        String estado = request.getParameter("ufPaciente");
               
        String idpaciente = request.getParameter("id");
        
        Paciente paciente = GerenciadorPaciente.buscarPaciente(Long.valueOf(idpaciente));
        
        paciente.setNome(nome);
        paciente.setRg(new RG(rg));
        paciente.setCpf(new CPF(cpf));
        paciente.setDataNascimento(new Date(dataNasc));
        paciente.setEmail(email);
        paciente.setEndereco(endereco);
        paciente.setCelular(fone);
        paciente.setCidade(cidade);
        paciente.setEstado(estado);
        
        GerenciadorPaciente.alterarPaciente(paciente);
                      
        RequestDispatcher view = request.getRequestDispatcher("PacienteServlet?page=listar");        
        view.forward(request, response);
    }

    private void excluirPaciente(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idpaciente = request.getParameter("id");
        
        Paciente paciente = GerenciadorPaciente.buscarPaciente(Long.valueOf(idpaciente));
        GerenciadorPaciente.removePaciente(paciente);  
        
        RequestDispatcher view = request.getRequestDispatcher("PacienteServlet?page=listar");        
        view.forward(request, response);
    }

    private void renderPaciente(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idpaciente = request.getParameter("id");
        Paciente paciente = GerenciadorPaciente.buscarPaciente(Long.valueOf(idpaciente));
        
        RequestDispatcher view = request.getRequestDispatcher("edit-paciente.jsp");
        request.setAttribute("paciente", paciente);
    
        view.forward(request, response);
    }
}
